{extends file="index.tpl"}
{block name=head_title}HỢP ĐỒNG{/block}
{block name=head_script}
    <script>
        $(document).ready(function () {
        {if isset($cbbExpiring)}
            $('#cbbExpiring').val({$cbbExpiring});
        {/if}
        {if isset($hieuluc)}
            $('#hieuluc').val({$hieuluc});
        {/if}
        {if $action == "edit"}
            $('#maDD').val({$hopdong[0].MaDD});
            $('#maHD').val('{$hopdong[0].MaHD|escape:'quotes'}');
            $('#phuongthucthanhtoan').val({$hopdong[0].MaPTTT});
            $('#thoigianbatdau').val('{$hopdong[0].ThoiGianBatDau}');
            $('#sothang').val({$hopdong[0].SoThang});
            $('#songaybaoduong').val({$hopdong[0].SoNgayBaoDuong});
            $('#gia').val({$hopdong[0].Gia});
            $('#soluongTV').val({$hopdong[0].SoLuong});
            $('#hieuluc').prop('checked',{$hopdong[0].HieuLuc});
            $('#chuTK').val('{$hopdong[0].ChuTK}');
            $('#soTK').val('{$hopdong[0].SoTK}');
            $('#tennganhang').val('{$hopdong[0].TenNH}');
        {/if}
        {if $action == "add" || $action == "edit"}
            $('#gia').priceFormat({
                centsLimit: false,
                prefix: false,
                limit: 7
            });
        {/if}
            $('#dataEmail').val($('#div_email').html());
        });
    </script>
{/block}
{block name=head_style}{/block}
{block name=body}
    {if isset($action)}
        {if $action == "expiring"}
            <!-- Table Add/Edit -->
            <form id="frmExpiring" onsubmit="" action="/hopdong/expiring" method="post">
                <table cellspacing="0" cellpadding="3" align="center" border="1" class="table">
                    <thead>
                        <tr class="table_thead_tr">
                            <th colspan="2">
                                {"Hợp đồng sắp hết hạn"|upper}
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr style="color:#000066;">
                            <td>Xem trong vòng</td>
                            <td>
                                <select id="cbbExpiring" name="cbbExpiring">
                                    <option value="0">Chọn</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                </select>
                                tháng
                            </td>
                        </tr>
                        <tr style="color:#000066;">
                            <td>Hiệu lực</td>
                            <td>
                                <select id="hieuluc" name="hieuluc">
                                    <option value="1" selected="selected">Còn</option>
                                    <option value="0">Hết</option>
                                    <option value="-1">Tất cả</option>
                                </select>
                                tháng
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align: center">  
                                <input type="submit" id="submit" name="submit" value="Xem" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>

            <br />
            <style>
                .red {
                    color: red;
                    font-weight: bold;
                }
            </style>
            <!-- Table View -->
            {if isset($hopdong)}
                <div id='div_email'>
                    <table cellspacing="0" cellpadding="3" align="center" border="1" class="table">
                        <thead>
                            <tr>
                                <th colspan="17" class="table_thead_tr">
                                    DANH SÁCH HỢP ĐỒNG
                                </th>
                            </tr>
                            <tr class="table_thead_tr">
                                <th rowspan="2" style="width: 145px">Contract Number</th>
                                <th rowspan="2" style="width: 145px">Cafe Name</th>
                                <th rowspan="2" style="width: 145px">City</th>
                                <th rowspan="2" style="width:65px">Rental<br/>Duration (months)</th>
                                <th rowspan="2" style="width: 95px">Rental Unit Price (month/LCD)</th>
                                <th rowspan="2" style="width: 75px">LCD Quantity (pcs/cafe)</th>
                                <th rowspan="2" style="width: 95px">Maintenance (days)</th>
                                <th rowspan="2" style="width: 80px">Total Rental Value (cafe/month)</th>
                                <th rowspan="2" style="width: 80px">Total Rental Value</th>
                                <th colspan="2">Contract Timing<br/>(effective date)</th>
                                <th rowspan="2" style="width: 65px">Payment method</th>
                                <th rowspan="2" style="width: 55px">Số đợt</th>
                                <th rowspan="2" style="width: 55px">Đợt TT</th>
                                <th rowspan="2" style="width: 55px">Valid</th>
                                <th rowspan="2" style="width: 45px">Notes</th>
                                <th rowspan="2" style="">Remaining Expiring Contract</th>
                            </tr>
                            <tr class="table_thead_tr">
                                <th style="width: 90px">Start</th>   
                                <th style="width: 90px">End</th>
                            </tr>
                        </thead>
                        <tbody>

                            {foreach from=$hopdong item=i}
                                <tr>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} title="{$i.TenDD}">{$i.MaHD}</td>     
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.TenTP}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.TenDD}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.SoThang}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: right">{$i.Gia|number_format}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.SoLuong}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.SoNgayBaoDuong}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: right">{($i.SoLuong * $i.Gia)|number_format}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: right">{($i.SoLuong * $i.Gia * $i.SoThang)|number_format}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.ThoiGianBatDau|date_format:"%d-%m-%Y"}</td>              
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.ThoiGianKetThuc|date_format:"%d-%m-%Y"}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.TenPTTT}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.SoDotThanhToan}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.TenDTT}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{if $i.HieuLuc == 1}Valid{else}Unvalid{/if}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.GhiChu}</td>
                                    <td {if $i.RemainingExpiring<=0}class="red"{/if} style="text-align: center">{$i.RemainingExpiring} days</td>
                                </tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>
            {else}
                {if !isset($error)}
                    <p style="font-size:20pt; text-align: center;">Không có dữ liệu!</p>
                {/if}
            {/if}
        {/if}
        {if $action == "add" || $action == "edit"}
            <!-- Table Add/Edit -->
            <form id="frmHopDong" onsubmit="" action="/hopdong" method="post">
                <table cellspacing="0" cellpadding="3" align="center" border="1" class="table">
                    <thead>
                        <tr class="table_thead_tr">
                            <th colspan="2">
                                {if $action == "add"}
                                    {"Thêm hợp đồng"|upper}
                                {/if}
                                {if $action == "edit"}
                                    {"Sửa hợp đồng"|upper}
                                {/if}
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>Tên địa điểm</td>
                            <td>
                                <select id="maDD" name="maDD" style="width: 200px">
                                    {if isset($diadiem)}
                                        <option value="0">Chọn</option>
                                        {foreach from=$diadiem item=i}
                                            <option value="{$i.MaDD}">{$i.TenDD}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Mã hợp đồng</td>
                            <td> 
                                <input id="maHD" name="maHD" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Phương thức thanh toán</td>
                            <td>
                                <select id="phuongthucthanhtoan" name="phuongthucthanhtoan" style="width: 200px">
                                    {if isset($phuongthucthanhtoan)}
                                        <option value="0">Chọn</option>
                                        {foreach from=$phuongthucthanhtoan item=i}
                                            <option value="{$i.MaPTTT}">{$i.TenPTTT}</option>
                                        {/foreach}
                                    {/if}
                                </select> 
                            </td>
                        </tr>
                        <tr>
                            <td>Thời gian bắt đầu</td>
                            <td> 
                                <input id="thoigianbatdau" name="thoigianbatdau" type="date" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Số tháng</td>
                            <td> 
                                <input id="sothang" name="sothang" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Số ngày bảo dưỡng</td>
                            <td> 
                                <input id="songaybaoduong" name="songaybaoduong" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Đơn giá/LCD/tháng</td>
                            <td> 
                                <input id="gia" name="gia" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Số lượng TV</td>
                            <td> 
                                <input id="soluongTV" name="soluongTV" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Hiệu lực</td>
                            <td> 
                                <input type="checkbox" id="hieuluc" name="hieuluc" value="1">Valid<br>
                            </td>
                        </tr>
                        <tr>
                            <td>Chủ TK</td>
                            <td> 
                                <input id="chuTK" name="chuTK" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Số TK</td>
                            <td> 
                                <input id="soTK" name="soTK" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td>Tên ngân hàng</td>
                            <td> 
                                <input id="tennganhang" name="tennganhang" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align: center">  
                                <input type="submit" id="submit" name="submit" value="{if $action == "add"}Add{else}Update{/if}" />
                            </td>
                        </tr>
                    </tbody>
                </table>
                {if $action == "edit"}
                    <input type="hidden" id="ID" name="ID" value="{$hopdong[0].ID}" />
                {/if}
            </form>
        {/if}
        {if $action == "expiring"}
            <br />
            <form id='frmExpiringEmail' action="/hopdong/expiring" method="post">
                <table align="center" border="0">
                    <tbody>
                        <tr>
                            <td>
                                <input type="hidden" id='dataEmail' name="dataEmail" value=''/>
                                <input type="submit" id="submit" name="submit" value="Send Mail" style="font-size:18pt">
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        {/if}
    {/if}

    <!-- Hiện thông báo sau khi xử lý dữ liệu -->
    {if isset($success)}
        <span>
            {if $success==1}
                <p style="color:green">Thao tác thành công!</p>
            {/if}
            {if $success==-1}
                <p style="color:red">Thao tác thất bại vì dữ liệu bị trùng hoặc không tồn tại!</p>
            {/if}
            {if $success==0}
                <p style="color:red">Thao tác thất bại vì dữ liệu đang sử dụng!</p>
            {/if}
            <p>Đang chuyển đến trang "Danh sách hợp đồng" sau: <span id="redirect" style="color: red; font-weight: bold">3</span> giây. 
                Hoặc <a href="/diadiem/view/0/hopdong">chuyển ngay</a>
            </p>
            <script>
                setTimeout(function () {
                    $("#redirect").text('' + 2);
                }, 1000);
                setTimeout(function () {
                    $("#redirect").text('' + 1);
                }, 2000);
                setTimeout(function () {
                    $("#redirect").text('' + 0);
                }, 3000);
                setTimeout(function () {
                    top.location.href = '/diadiem/view/0/hopdong';
                }, 3500);
            </script>
        </span>
    {/if}
    {if isset($error)}
        {if $error!="0"}
            <p style="font-size: 20pt; text-align: center; color: red">Mailer error: {$error}</p>
        {else}
            <p style="font-size:20pt; text-align: center;">Gửi email thành công!</p>
        {/if}
    {/if}
{/block}
